package imotech.ejb.dao;

import java.util.List;

import imotech.ejb.model.Imovel;
import imotech.ejb.model.ImovelBusca;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class ImovelDAOBean implements ImovelDAO {

	@PersistenceContext(unitName="imotech-ejb")
	private EntityManager em;
	
	@Override
	public void cadastrar(Imovel imovel) {
		em.persist(imovel);
	}
	
	@Override
	public List<Imovel> todos() {
		return em.createNamedQuery("findTodosImoveis", Imovel.class).getResultList();
	}

	@Override
	public Imovel recuperar(int id) {
		return em.find(Imovel.class, id);
	}

	@Override
	public List<Imovel> buscar(ImovelBusca criterio) {
		return em.createNamedQuery("buscar", Imovel.class)
				.setParameter("negociacao", criterio.getNegociacao())
				.setParameter("tipo", criterio.getTipo())
				.setParameter("bairro", criterio.getBairro())
				.setParameter("valorMinimo", criterio.getValorMinimo())
				.setParameter("valorMaximo", criterio.getValorMaximo())
				.setParameter("quartosMinimo", criterio.getQuartosMinimo())
				.setParameter("quartosMaximo", criterio.getQuartosMaximo())
				.getResultList();
	}

}
